package userInterface.Panels.CommandArgumentPanels;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;

/**
 * {@link userInterface.panels.CommandArgumentPanels.PanelCommandArguments PanelCommandArguments} for editing
 * {@link settings.Action.Action#TEXTENTRY Action.TEXTENTRY} {@link settings.Command Command} arguments.
 * @author BDS
 */
public class PanelTextEntryArguments extends PanelCommandArguments
{
	private static final long serialVersionUID = 1L;
	
	private GridBagLayout gblTextEntryArguments;
	
	private JLabel lblTextEntry;
	private JFormattedTextField txtTextEntry;
	private JLabel lblDate;
	private JLabel lblTime;
	private JLabel lblIteration;

	/**
	 * Class constructor.
	 */
	public PanelTextEntryArguments()
	{
		gblTextEntryArguments = new GridBagLayout();
		gblTextEntryArguments.columnWidths = new int[] { 100, 0 } ;
		gblTextEntryArguments.columnWeights = new double[] { 0.0, 0.0, 1.0 };
		setLayout(gblTextEntryArguments);

		lblTextEntry = new JLabel("Text:");
        GridBagConstraints gbc_lblTextEntry = new GridBagConstraints();
        gbc_lblTextEntry.anchor = GridBagConstraints.WEST;
        gbc_lblTextEntry.insets = new Insets(0, 0, 5, 5);
        gbc_lblTextEntry.gridx = 0;
        gbc_lblTextEntry.gridy = 0;
        add(lblTextEntry, gbc_lblTextEntry);

		txtTextEntry = new JFormattedTextField();
        GridBagConstraints gbc_txtTextEntry = new GridBagConstraints();
        gbc_txtTextEntry.fill = GridBagConstraints.HORIZONTAL;
        gbc_txtTextEntry.insets = new Insets(0, 0, 5, 0);
        gbc_txtTextEntry.gridwidth = 2;
        gbc_txtTextEntry.gridx = 1;
        gbc_txtTextEntry.gridy = 0;
        add(txtTextEntry, gbc_txtTextEntry);
		
		lblDate = new JLabel("%D - Date");
        GridBagConstraints gbc_lblDate = new GridBagConstraints();
        gbc_lblDate.anchor = GridBagConstraints.WEST;
        gbc_lblDate.insets = new Insets(0, 0, 5, 5);
        gbc_lblDate.gridwidth = 2;
        gbc_lblDate.gridx = 0;
        gbc_lblDate.gridy = 1;
        add(lblDate, gbc_lblDate);
		
		lblTime = new JLabel("%T - Time");
        GridBagConstraints gbc_lblTime = new GridBagConstraints();
        gbc_lblTime.anchor = GridBagConstraints.WEST;
        gbc_lblTime.insets = new Insets(0, 0, 5, 5);
        gbc_lblTime.gridwidth = 2;
        gbc_lblTime.gridx = 0;
        gbc_lblTime.gridy = 2;
        add(lblTime, gbc_lblTime);
		
		lblIteration = new JLabel("%I - Workflow iteration");
        GridBagConstraints gbc_lblIteration = new GridBagConstraints();
        gbc_lblIteration.anchor = GridBagConstraints.WEST;
        gbc_lblIteration.insets = new Insets(0, 0, 0, 5);
        gbc_lblIteration.gridwidth = 2;
        gbc_lblIteration.gridx = 0;
        gbc_lblIteration.gridy = 3;
        add(lblIteration, gbc_lblIteration);
	}
	
    /**
     * Sets the enabled state of contained controls.
     * @param enabled Determines the enabled state of contained controls.
     */
	@Override
	public void setEnabled(boolean enabled)
	{
        super.setEnabled(enabled);
        
		lblTextEntry.setEnabled(enabled);
		txtTextEntry.setEnabled(enabled);
		lblDate.setEnabled(enabled);
		lblTime.setEnabled(enabled);
		lblIteration.setEnabled(enabled);
	}
	
    /**
     * Provides the external event processing class references to the contained {@link javax.swing.JComponent JComponents}.
     * @param actionListener The {@link java.awt.event.ActionListener ActionListener} for {@link javax.swing.JComboBox JComboBoxes}.
     * @param focusAdapter The {@link java.awt.event.FocusAdapter FocusAdapter} for {@link javax.swing.JFormattedTextField JFormattedTextFields}.
     * @param keyAdapter The {@link java.awt.event.KeyAdapater KeyAdapater} for {@link javax.swing.JFormattedTextField JFormattedTextFields}.
     * @param changeListener The {@link javax.swing.event.ChangeListener ChangeListener} for {@link javax.swing.JSpinner JSpinners}.
     */
	@Override
	public void addActionListeners(ActionListener actionListener, FocusAdapter focusAdapter, KeyAdapter keyAdapter, ChangeListener changeListener)
	{ 
		txtTextEntry.addFocusListener(focusAdapter);
		txtTextEntry.addKeyListener(keyAdapter);
	}
	
	/**
	 * Gets the {@link settings.Action.Action#TEXTENTRY Action.TEXTENTRY} {@link settings.Command} arguments currently displayed.
	 * @return The {@link settings.Action.Action#TEXTENTRY Action.TEXTENTRY} {@link settings.Command} arguments currently displayed.
	 */
	@Override
	public List<String> getArguments() 
	{ 
		List<String> arguments = new ArrayList<String>();
		
		arguments.add(txtTextEntry.getText());
		
		return arguments;
	}
	
    /**
     * <ul>
     *     <li>Sets the {@link settings.Action.Action#TEXTENTRY Action.TEXTENTRY} {@link settings.Command Command} arguments.</li>
     *     <li>Updates the UI based on the given arguments.</li>
     * </ul>
     * @param arguments The {@link settings.Action.Action#TEXTENTRY Action.TEXTENTRY} {@link settings.Command Command} arguments used to update the UI.
     */
    @Override
	public void setArguments(List<String> arguments) 
	{
		if (arguments != null && arguments.size() > 0 && arguments.get(0) != null) 
		{
			txtTextEntry.setText(arguments.get(0));
		}
		else
		{
			txtTextEntry.setText("");
		}
	}
}
